package com.commpany.servie.basic.promotion.service.read;

import com.commpany.servie.basic.promotion.model.dto.productpromotionmap.ProductPromotionMapDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.ArrayList;
import com.commpany.servie.basic.promotion.api.read.PromotionReadService;
import com.commpany.jdq.core.common.PageResult;
import com.commpany.jdq.core.orm.Pagination;
import com.commpany.servie.basic.promotion.business.read.promotion.PromotionReadManage;
import com.commpany.servie.basic.promotion.model.dto.promotion.PromotionDTO;
import com.commpany.servie.basic.promotion.converter.PromotionConverter;
import com.commpany.servie.basic.promotion.po.PromotionPO;
import com.commpany.servie.basic.promotion.model.dto.PageResultDTO;
@Service("promotionReadService")
public class PromotionReadServiceImpl  implements PromotionReadService {
	@Autowired
	private PromotionReadManage promotionReadManage;
	
	public PromotionDTO getPromotionService(PromotionDTO inputDTO){
	
	   return PromotionConverter.toDTO(promotionReadManage.getPromotionService(PromotionConverter.toPO(inputDTO)));
	}
   @Override
	public PageResultDTO<PromotionDTO> selectPromotionPageService(PromotionDTO inputDTO){

		PromotionPO po = PromotionConverter.toPO(inputDTO);
		Pagination page = new Pagination();
	    page.setLimitStart(inputDTO.getLimitStart());
		page.setPageNo(inputDTO.getPageNo());
		page.setPageSize(inputDTO.getPageSize());
		Optional.ofNullable(inputDTO.getOrderBy()).ifPresent(page::setOrderBy);
		PageResult<PromotionPO> poPage = promotionReadManage.selectPromotionPageService( po,page);
		List<PromotionPO> poList = poPage.getList();
		if(Objects.isNull(poList)) {
	    	 return PageResultDTO.<PromotionDTO>builder().build();
	     }		
		 List<PromotionDTO> dtoList = poList.stream().map(src-> PromotionConverter.toDTO(src)).collect(Collectors.toList());
		return 	PageResultDTO.<PromotionDTO>builder().pageNo(poPage.getPageNo()).pageSize(poPage.getPageSize()).list(dtoList).build();
	}
   @Override
    public int countPromotionService( PromotionDTO inputDTO ){
         PromotionPO po = PromotionConverter.toPO(inputDTO);
         return promotionReadManage.countPromotionService(po);
    }
     @Override
	public List<PromotionDTO> queryPromotionService (PromotionDTO inputDTO){
	    PromotionPO po = PromotionConverter.toPO(inputDTO);
	     List<PromotionPO> list = promotionReadManage.queryPromotionService(po);
		   if(Objects.isNull(list)) {
	    	 return new ArrayList<PromotionDTO>();
	     }
		return list.stream().map(src-> PromotionConverter.toDTO(src)).collect(Collectors.toList());
	}

	@Override
	public List<String> queryPromotionListByProductID (Integer productId){

		return null;
	}
}
	